package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.mapper.CarPackageAuditMapper;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
public class ServiceItemServiceImpl implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private CarPackageAuditMapper carPackageAuditMapper;
    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;
    @Autowired
    private RuntimeService runtimeService;


    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<ServiceItem>(serviceItemMapper.selectForList(qo));
    }


    @Override
    public void save(ServiceItem serviceItem) {
/*        ServiceItem item = new ServiceItem();
        item.setName(serviceItem.getName());
        item.setOriginalPrice(serviceItem.getOriginalPrice());
        item.setDiscountPrice(serviceItem.getDiscountPrice());
        item.setCarPackage(serviceItem.getCarPackage());
        item.setServiceCatalog(serviceItem.getServiceCatalog());
        item.setInfo(serviceItem.getInfo());*/
        //自己设置创建的时间到对象里面去
        serviceItem.setCreateTime(new Date());
//        item.setCreateTime(new Date());
        //如果是套餐将其审核状态设置为初始化.否则就设置为无需审核状态
        if (serviceItem.getCarPackage()) {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
//            item.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }
        //防止字段的注入,我们需要自己设置初始化的上架状态为下架状态
        serviceItem.setSaleStatus(ServiceItem.SALESTATUS_OFF);
//        item.setSaleStatus(ServiceItem.SALESTATUS_OFF);

        serviceItemMapper.insert(serviceItem);
    }

    @Override
    public ServiceItem get(Long id) {
        return serviceItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(ServiceItem serviceItem) {
        //用id查询出来该条数据的对象
        ServiceItem dbItem = this.get(serviceItem.getId());
        //如果该条数据在上架中或者是审核中我们应该抛出异常
        if (dbItem.getSaleStatus().equals(ServiceItem.SALESTATUS_ON)
                || dbItem.getAuditStatus().equals(ServiceItem.AUDITSTATUS_AUDITING)){
            throw new BusinessException("非法操作");
        }
        //如果是初始化的情况,那么会把审核状态设置成无需审核
        //我们需要将原来的审核状态设置回去给对象
        serviceItem.setAuditStatus(dbItem.getAuditStatus());
        //如果该条数据使套餐且审核通过了,我们应该将其审核状态设置成初始化
        if (dbItem.getCarPackage() && dbItem.getAuditStatus().equals(ServiceItem.AUDITSTATUS_APPROVED)){
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }

        serviceItemMapper.updateByPrimaryKey(serviceItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            serviceItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<ServiceItem> list() {
        return serviceItemMapper.selectAll();
    }

    @Override
    public void saleOn(Long id) {
        //根据id查询出该条数据
        ServiceItem dbItem = this.get(id);
        //如果不是下架状态就抛出异常
        if (dbItem.getSaleStatus().equals(ServiceItem.SALESTATUS_ON)){
            throw new BusinessException("非法操作");
        }
        //如果是套餐且不是在审核通过的状态同样的抛出异常
        if (dbItem.getCarPackage() && !(dbItem.getAuditStatus().equals(ServiceItem.AUDITSTATUS_APPROVED))){
            throw new BusinessException("非法操作");
        }
        //将数据修改为上架状态
        serviceItemMapper.changeSaleStatus(id,ServiceItem.SALESTATUS_ON);
    }

    @Override
    public void saleOff(Long id) {
        //根据id查询出该条数据
        ServiceItem dbItem = this.get(id);

        //如果不是下架状态就抛出异常
        if (dbItem.getSaleStatus().equals(ServiceItem.SALESTATUS_OFF)){
            throw new BusinessException("非法操作");
        }

        //将数据修改为上架状态
        serviceItemMapper.changeSaleStatus(id,ServiceItem.SALESTATUS_OFF);
    }

    @Override
    public void startAudit(Long id, Long bpmnInfoId, Long director, Long finance, String info) {
        //根据id查询serviceItem服务单项的数据
        ServiceItem serviceItem = serviceItemMapper.selectByPrimaryKey(id);
        //判断状态是否是审核中的状态或者是审核通过的状态
        if (ServiceItem.AUDITSTATUS_AUDITING.equals(serviceItem.getAuditStatus())
                || ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())){
            throw new BusinessException("非法操作");
        }
        //创建一个CarPackageAudit对象将数据封装进去
        CarPackageAudit carPackageAudit = new CarPackageAudit();
        //设置备注信息
        carPackageAudit.setInfo(info);
        //设置优惠价格 就是套餐价格
        carPackageAudit.setServiceItemPrice(serviceItem.getDiscountPrice());
        //设置套餐备注
        carPackageAudit.setServiceItemInfo(serviceItem.getInfo());
        //设置套餐的id
        carPackageAudit.setServiceItemId(serviceItem.getId());
        //设置发起审批人的名称
        carPackageAudit.setCreator(ShiroUtils.getUser().getUserName());
        //设置bpmnInfo的id
        carPackageAudit.setBpmnInfoId(bpmnInfoId);
        //设置创建时间
        carPackageAudit.setCreateTime(new Date());
        carPackageAudit.setAuditorId(director);
        //将数据插入到数据库中
        carPackageAuditMapper.insert(carPackageAudit);
        //开启流程实例之前查出来一个bpmnInfo信息
        BpmnInfo bpmnInfo = bpmnInfoMapper.selectByPrimaryKey(bpmnInfoId);
        // 设置流程变量
        Map<String,Object> map = new HashMap<>();
        map.put("director",director);
        if (finance!=null){
            map.put("finance",finance);
        }
        map.put("discountPrice",serviceItem.getDiscountPrice().longValue());
        //
        //开启流程实例
        //启动流程实例.获取流程实例id,获取到实例id后更新carpackageAudit对象的流程实例id
        ProcessInstance processInstance = runtimeService
                .startProcessInstanceByKey(bpmnInfo.getActProcessKey(), carPackageAudit.getId() + "", map);

        carPackageAudit.setInstanceId(processInstance.getId());
        carPackageAuditMapper.updateByPrimaryKey(carPackageAudit);
        //修改服务单项状态为审核中状态
        serviceItemMapper.changeAuditStutas(id,ServiceItem.AUDITSTATUS_AUDITING);

    }

}
